package com.yanqu.road.server.manager.activity.xingyou.inter.grid;

import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiTeamMapData;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiUserData;
import com.yanqu.road.entity.activity.youxing.ShanShuiGridParam;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiGridInfo;
import com.yanqu.road.entity.log.cross.xingyou.LogCrossXingYouBuilding;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.manager.activity.xingyou.DiceHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.enums.eXingYouGridTypes;
import com.yanqu.road.server.manager.activity.xingyou.inter.GridHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.inter.IXingYouGridHandler;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.Collections;

/**
 * 建筑格
 */
public class BuildGridHandler implements IXingYouGridHandler {

    @Override
    public void handle(XingYouShanShuiGridInfo gridInfo, int pos, int gridId, int endPos, boolean fly, DiceHandleResult result) {
        //是否停留
        boolean stop = pos == endPos;

        GridHandleResult gridHandleResult = new GridHandleResult();
        //建筑是否修建
        CrossXingYouShanShuiTeamMapData teamMapData = result.getActivity().getCrossXingYouShanShuiTeamMapData(result.getUserData().getTeamId(), pos);

        if(result.getUserData().isDrift()){
            if(result.getUserData().getMapTeamId() > 0){
                teamMapData = result.getActivity().getCrossXingYouShanShuiTeamMapData(result.getUserData().getMapTeamId(), pos);
                if(stop && teamMapData.getBuildLv() > 0){
                    //给对方闭园
                    XingYouShanShuiGridInfo yumenGridInfo = result.getConfig().getGridInfo(eXingYouGridTypes.Sixteen.getId());
                    synchronized (result.getActivity().getTeamLocker(result.getUserData().getMapTeamId())){
                        Integer closeTimes = yumenGridInfo.getParam().get(3);
                        teamMapData.setCloseTimes(closeTimes);

                        LogCrossXingYouBuilding logData = new LogCrossXingYouBuilding(result.getUserData().getActivityId(), result.getUserData().getTeamId(), result.getUserData().getUserId(),
                                pos, gridInfo.getId(), 1, result.getUserData().getMapTeamId(), closeTimes, closeTimes);
                        result.getAutoLogList().add(logData);

                        result.getActivity().syncShanShuiPropNotify(result.getUserData().getMapTeamId(), Collections.singletonList(gridId));
                    }
                }
            }
        }else {
//            if(stop){
                //减少闭园次数
                synchronized (result.getActivity().getTeamLocker(result.getUserData().getTeamId())){
                    if(teamMapData.getCloseTimes() > 0){
                        teamMapData.setCloseTimes(teamMapData.getCloseTimes() - 1);
                        LogCrossXingYouBuilding logData = new LogCrossXingYouBuilding(result.getUserData().getActivityId(), result.getUserData().getTeamId(), result.getUserData().getUserId(),
                                pos, gridInfo.getId(), 2, result.getUserData().getTeamId(), 1, teamMapData.getCloseTimes());
                        result.getAutoLogList().add(logData);

                        result.getActivity().syncShanShuiPropNotify(result.getUserData().getTeamId(), Collections.singletonList(gridId));
                    }
                }
//            }
        }

        XingYouShanShuiProto.ShanShuiEventInfo.Builder builder = XingYouShanShuiProto.ShanShuiEventInfo.newBuilder();
        Property property = result.getActivity().getBuildingReward(gridInfo, result.getConfig(), teamMapData.getBuildLv(),
                result.getUserData().isDrift() ? 0 : teamMapData.getCloseTimes(), result.getUserData().isDrift(), stop);

        Property fixedReward = result.getActivity().getGridInfoFixedReward(result.getUserData(), gridInfo, stop);
        builder.setFixedReward(PropertyHelper.parsePropertyToString(fixedReward));
        property.addProperty(fixedReward);

        builder.setBaseReward(PropertyHelper.parsePropertyToString(property));
        if(!property.isNothing()){
            //漂泊不带buff
            if(!result.getUserData().isDrift()) {
                int roleBuffAddition = result.getActivity().getRoleBuffAddition(result.getUserData(), 1);
                //事件跳转的
                if(result.isEventGridJump()){
                    roleBuffAddition += result.getActivity().getRoleBuffAddition(result.getUserData(), 2);
                }
                result.getActivity().calRewardBuff(result.getUserData(), property, result.isInMazu(), roleBuffAddition, true);
            }

            builder.setPosition(pos);
            builder.setGridId(gridId);
            builder.setAward(PropertyHelper.parsePropertyToString(property));
            gridHandleResult.setBuilder(builder);
        }else {

            builder.setPosition(pos);
            builder.setGridId(gridId);
            gridHandleResult.setBuilder(builder);
        }

        ShanShuiGridParam gridParam = new ShanShuiGridParam(gridId, pos, true, result.getUserData().isDrift());
        if(!result.getUserData().isDrift() && stop && teamMapData.getBuildLv() < gridInfo.getParam().get(2)){
            gridParam.setHandled(false);
        }
        gridHandleResult.setGridParam(gridParam);

        result.getGridHandleResultList().add(gridHandleResult);

        CrossXingYouShanShuiUserData userData = result.getUserData();
        //是否有捕快
        if(stop) {
            result.getActivity().tryCatch(pos, userData, result, gridHandleResult);
        }
    }
}
